home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 February / CMCD0205.ISO / Software / Freeware / Programare / bluej / bluejsetup-203.exe / {app} / examples / shapes / Circle.java < prev    next >
Text File  |  2004-12-19  |  3KB  |  193 lines

  1. import java.awt.*;
  2. import java.awt.geom.*;
  3.  
  4. /**
  5.  * A circle that can be manipulated and that draws itself on a canvas.
  6.  * 
  7.  * @author    Michael Kolling and David J. Barnes
  8.  * @version 1.0  (15 July 2000)
  9.  */
  10.  
  11. public class Circle
  12. {
  13.     private int diameter;
  14.     private int xPosition;
  15.     private int yPosition;
  16.     private String color;
  17.     private boolean isVisible;
  18.     
  19.     /**
  20.      * Create a new circle at default position with default color.
  21.      */
  22.     public Circle()
  23.     {
  24.         diameter = 30;
  25.         xPosition = 20;
  26.         yPosition = 60;
  27.         color = "blue";
  28.         isVisible = false;
  29.     }
  30.  
  31.     /**
  32.      * Make this circle visible. If it was already visible, do nothing.
  33.      */
  34.     public void makeVisible()
  35.     {
  36.         isVisible = true;
  37.         draw();
  38.     }
  39.     
  40.     /**
  41.      * Make this circle invisible. If it was already invisible, do nothing.
  42.      */
  43.     public void makeInvisible()
  44.     {
  45.         erase();
  46.         isVisible = false;
  47.     }
  48.     
  49.     /**
  50.      * Move the circle a few pixels to the right.
  51.      */
  52.     public void moveRight()
  53.     {
  54.         moveHorizontal(20);
  55.     }
  56.  
  57.     /**
  58.      * Move the circle a few pixels to the left.
  59.      */
  60.     public void moveLeft()
  61.     {
  62.         moveHorizontal(-20);
  63.     }
  64.  
  65.     /**
  66.      * Move the circle a few pixels up.
  67.      */
  68.     public void moveUp()
  69.     {
  70.         moveVertical(-20);
  71.     }
  72.  
  73.     /**
  74.      * Move the circle a few pixels down.
  75.      */
  76.     public void moveDown()
  77.     {
  78.         moveVertical(20);
  79.     }
  80.  
  81.     /**
  82.      * Move the circle horizontally by 'distance' pixels.
  83.      */
  84.     public void moveHorizontal(int distance)
  85.     {
  86.         erase();
  87.         xPosition += distance;
  88.         draw();
  89.     }
  90.  
  91.     /**
  92.      * Move the circle vertically by 'distance' pixels.
  93.      */
  94.     public void moveVertical(int distance)
  95.     {
  96.         erase();
  97.         yPosition += distance;
  98.         draw();
  99.     }
  100.  
  101.     /**
  102.      * Slowly move the circle horizontally by 'distance' pixels.
  103.      */
  104.     public void slowMoveHorizontal(int distance)
  105.     {
  106.         int delta;
  107.  
  108.         if(distance < 0) 
  109.         {
  110.             delta = -1;
  111.             distance = -distance;
  112.         }
  113.         else 
  114.         {
  115.             delta = 1;
  116.         }
  117.  
  118.         for(int i = 0; i < distance; i++)
  119.         {
  120.             xPosition += delta;
  121.             draw();
  122.         }
  123.     }
  124.  
  125.     /**
  126.      * Slowly move the circle vertically by 'distance' pixels.
  127.      */
  128.     public void slowMoveVertical(int distance)
  129.     {
  130.         int delta;
  131.  
  132.         if(distance < 0) 
  133.         {
  134.             delta = -1;
  135.             distance = -distance;
  136.         }
  137.         else 
  138.         {
  139.             delta = 1;
  140.         }
  141.  
  142.         for(int i = 0; i < distance; i++)
  143.         {
  144.             yPosition += delta;
  145.             draw();
  146.         }
  147.     }
  148.  
  149.     /**
  150.      * Change the size to the new size (in pixels). Size must be >= 0.
  151.      */
  152.     public void changeSize(int newDiameter)
  153.     {
  154.         erase();
  155.         diameter = newDiameter;
  156.         draw();
  157.     }
  158.  
  159.     /**
  160.      * Change the color. Valid colors are "red", "yellow", "blue", "green",
  161.      * "magenta" and "black".
  162.      */
  163.     public void changeColor(String newColor)
  164.     {
  165.         color = newColor;
  166.         draw();
  167.     }
  168.  
  169.     /*
  170.      * Draw the circle with current specifications on screen.
  171.      */
  172.     private void draw()
  173.     {
  174.         if(isVisible) {
  175.             Canvas canvas = Canvas.getCanvas();
  176.             canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition, 
  177.                                                           diameter, diameter));
  178.             canvas.wait(10);
  179.         }
  180.     }
  181.  
  182.     /*
  183.      * Erase the circle on screen.
  184.      */
  185.     private void erase()
  186.     {
  187.         if(isVisible) {
  188.             Canvas canvas = Canvas.getCanvas();
  189.             canvas.erase(this);
  190.         }
  191.     }
  192. }
  193.